﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Navigation;
using IkeCode.WP7.Wakeup.Common;
using System.Collections.Generic;
using IkeCode.WP7.Wakeup.Service;
using IkeCode.WP7.Wakeup.AppModel.Enum;
using System.Linq;

namespace IkeCode.WP7.Wakeup.ViewModel
{
    public class SelectDaysViewModel : BaseViewModel
    {
        private readonly IDataService _dataService;
        private readonly INavigationService NavigationService;

        public SelectDaysViewModel(IDataService dataService, INavigationService navigationService)
        {
            _dataService = dataService;
            NavigationService = navigationService;
            this.SelectedDays = new List<Days>();
        }

        public override void SetData(object data)
        {
            base.SetData(data);
        }

        #region Properties

        private List<Days> _selectedDays;
        private List<Days> SelectedDays
        {
            get
            {
                //.ToArray().ToList() => 'workaround' to remove duplicate entries of a list of days
                if (_selectedDays != null)
                    return _selectedDays.ToArray().ToList();

                return new List<Days>();
            }
            set
            {
                if (_selectedDays == value)
                    return;

                _selectedDays = value;

                ToggleCheckUncheckStates();
            }
        }

        private bool _checkAllIsEnabled = true;
        public bool CheckAllIsEnabled
        {
            get { return _checkAllIsEnabled; }
            set
            {
                if (_checkAllIsEnabled == value)
                    return;

                _checkAllIsEnabled = value;
                RaisePropertyChanged(() => CheckAllIsEnabled);
            }
        }

        private bool _uncheckAllIsEnabled = false;
        public bool UncheckAllIsEnabled
        {
            get { return _uncheckAllIsEnabled; }
            set
            {
                if (_uncheckAllIsEnabled == value)
                    return;

                _uncheckAllIsEnabled = value;
                RaisePropertyChanged(() => UncheckAllIsEnabled);
            }
        }

        #region Days

        private bool _mondayIsChecked = false;
        public bool MondayIsChecked
        {
            get { return _mondayIsChecked; }
            set
            {
                if (_mondayIsChecked == value)
                    return;

                _mondayIsChecked = value;

                if (value)
                    _selectedDays.Add(Days.Monday);
                else
                    _selectedDays.Remove(Days.Monday);

                ToggleCheckUncheckStates();

                RaisePropertyChanged(() => MondayIsChecked);
            }
        }

        private bool _tuesdayIsChecked = false;
        public bool TuesdayIsChecked
        {
            get { return _tuesdayIsChecked; }
            set
            {
                if (_tuesdayIsChecked == value)
                    return;

                _tuesdayIsChecked = value;

                if (value)
                    _selectedDays.Add(Days.Tuesday);
                else
                    _selectedDays.Remove(Days.Tuesday);

                ToggleCheckUncheckStates();

                RaisePropertyChanged(() => TuesdayIsChecked);
            }
        }

        private bool _wednesdayIsChecked = false;
        public bool WednesdayIsChecked
        {
            get { return _wednesdayIsChecked; }
            set
            {
                if (_wednesdayIsChecked == value)
                    return;

                _wednesdayIsChecked = value;

                if (value)
                    _selectedDays.Add(Days.Wednesday);
                else
                    _selectedDays.Remove(Days.Wednesday);

                ToggleCheckUncheckStates();

                RaisePropertyChanged(() => WednesdayIsChecked);
            }
        }

        private bool _thursdayIsChecked = false;
        public bool ThursdayIsChecked
        {
            get { return _thursdayIsChecked; }
            set
            {
                if (_thursdayIsChecked == value)
                    return;

                _thursdayIsChecked = value;

                if (value)
                    _selectedDays.Add(Days.Thursday);
                else
                    _selectedDays.Remove(Days.Thursday);

                ToggleCheckUncheckStates();

                RaisePropertyChanged(() => ThursdayIsChecked);
            }
        }

        private bool _fridayIsChecked = false;
        public bool FridayIsChecked
        {
            get { return _fridayIsChecked; }
            set
            {
                if (_fridayIsChecked == value)
                    return;

                _fridayIsChecked = value;

                if (value)
                    _selectedDays.Add(Days.Friday);
                else
                    _selectedDays.Remove(Days.Friday);

                ToggleCheckUncheckStates();

                RaisePropertyChanged(() => FridayIsChecked);
            }
        }

        private bool _saturdayIsChecked = false;
        public bool SaturdayIsChecked
        {
            get { return _saturdayIsChecked; }
            set
            {
                if (_saturdayIsChecked == value)
                    return;

                _saturdayIsChecked = value;

                if (value)
                    _selectedDays.Add(Days.Saturday);
                else
                    _selectedDays.Remove(Days.Saturday);

                ToggleCheckUncheckStates();

                RaisePropertyChanged(() => SaturdayIsChecked);
            }
        }

        private bool _sundayIsChecked = false;
        public bool SundayIsChecked
        {
            get { return _sundayIsChecked; }
            set
            {
                if (_sundayIsChecked == value)
                    return;

                _sundayIsChecked = value;

                if (value)
                    _selectedDays.Add(Days.Sunday);
                else
                    _selectedDays.Remove(Days.Sunday);

                ToggleCheckUncheckStates();

                RaisePropertyChanged(() => SundayIsChecked);
            }
        }

        #endregion

        #endregion

        #region Commands

        public ICommand BarSpecialCommand
        {
            get
            {
                return new RelayCommand((obj) =>
                {
                    var param = Convert.ToInt32(obj);

                    if (param == 0)
                    {
                        this.ChangeBusinessDaysState(true);
                        this.ChangeWeekendDaysState(true);
                    }
                    else if (param == 1)
                    {
                        this.ChangeBusinessDaysState(true);
                        this.ChangeWeekendDaysState(false);
                    }
                    else if (param == 2)
                    {
                        this.ChangeBusinessDaysState(false);
                        this.ChangeWeekendDaysState(true);
                    }
                    else if (param == 3)
                    {
                        this.ChangeBusinessDaysState(false);
                        this.ChangeWeekendDaysState(false);
                    }

                    ToggleCheckUncheckStates();
                });
            }
        }

        public ICommand BackCommand
        {
            get
            {
                return new RelayCommand((obj) =>
                {
                    Messenger.Default.Send<List<Days>>(this.SelectedDays, "SelectedDays");
                    NavigationService.GoBack();
                });
            }
        }

        #endregion

        #region Private Methods

        private void ChangeBusinessDaysState(bool isChecked)
        {
            this.MondayIsChecked = isChecked;
            this.TuesdayIsChecked = isChecked;
            this.WednesdayIsChecked = isChecked;
            this.ThursdayIsChecked = isChecked;
            this.FridayIsChecked = isChecked;
        }

        private void ChangeWeekendDaysState(bool isChecked)
        {
            this.SaturdayIsChecked = isChecked;
            this.SundayIsChecked = isChecked;
        }

        private void ToggleCheckUncheckStates()
        {
            this.UncheckAllIsEnabled = SelectedDays.Count > 0;
            this.CheckAllIsEnabled = SelectedDays.Count < 7;
        }

        #endregion
    }
}
